package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.ManageKehuActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.TabEntity;
import com.eqdd.yiqidian.ui.gongneng.kehu.fragment.FankuiRecordFragment;
import com.eqdd.yiqidian.ui.gongneng.kehu.fragment.HuiFangRecordFragment;
import com.eqdd.yiqidian.ui.gongneng.kehu.fragment.JjhuiFragment;
import com.eqdd.yiqidian.ui.gongneng.kehu.fragment.LianxirenFragment;
import com.eqdd.yiqidian.ui.gongneng.kehu.fragment.SaleChanceFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.ArrayList;

public class ManageKehuActivity extends BaseActivity {
    private ManageKehuActivityCustom dataBinding;
    String[] titles = new String[]{"销售机会", "回访记录", "反馈记录", "联系人"};
    Class[] addClasses = new Class[]{
            AddSaleChanceActivity.class,
            AddHuifangRecordActivity.class,
            AddFankuiRecordActivity.class,
            AddLianxirenActivity.class
    };
    int[] requestCode = new int[]{
            Config.ADD_XIAOSHOU_RECORD,
            Config.ADD_HUIFANG_RECORD,
            Config.ADD_FANKUI_RECORD,
            Config.ADD_LIANXIREN
    };
    int[] selectedRes = new int[]{R.mipmap.chance, R.mipmap.huifang, R.mipmap.fankui, R.mipmap.myself};
    int[] unSelectedRes = new int[]{R.mipmap.chance, R.mipmap.huifang, R.mipmap.fankui, R.mipmap.myself};
    private String cusid;
    private ArrayList<Fragment> fragments;
    private String clientName;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_manage_kehu);
        initTopTitleBar(View.VISIBLE, "管理客户");
        initTopRightText("添加", v -> {
            Bundle bundle = new Bundle();
            bundle.putString("cusid", cusid);
            bundle.putString(Config.CLIENT_NAME, clientName);
            int currentItem = dataBinding.viewPager.getCurrentItem();
            startActivityForResult(addClasses[currentItem], bundle, requestCode[currentItem]);

        });
    }

    @Override
    public void initData() {
        cusid = getIntent().getStringExtra("cusid");
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            TabEntity tabEntity = new TabEntity(titles[i], selectedRes[i], unSelectedRes[i]);
            tabEntities.add(tabEntity);
        }
        dataBinding.commonTabLayout.setTabData(tabEntities);
        fragments = new ArrayList<>();
        fragments.add(createFragment(new SaleChanceFragment()));
        fragments.add(createFragment(new HuiFangRecordFragment()));
        fragments.add(createFragment(new FankuiRecordFragment()));
        fragments.add(createFragment(new LianxirenFragment()));
        dataBinding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), null, fragments));
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                dataBinding.commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        dataBinding.commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private Fragment createFragment(Fragment fragment) {
        Bundle bundle = new Bundle();
        bundle.putString("cusid", cusid);
        bundle.putString(Config.CLIENT_NAME, clientName);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragments.get(dataBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }
}
